<?php
/**
 * 菜单功能展示
 * 多级菜单展示、菜单相关操作
 * @author: yanxs <541139655@qq.com>
 * @version: 1.0
 * @date: 2021/3/22
 */

namespace App\Http\Controllers\Backend\Auth;

use App\Exceptions\CommonException;
use App\Http\Controllers\Backend\BaseController;
use App\Http\Requests\Backend\Auth\RuleRequest;
use App\Repositories\PrtAuthRuleRepository;
use App\Services\Helper;
use Illuminate\Http\Request;

/**
 * 操作菜单相关功能项
 * @author:yanxs<541139655@qq.com>
 * @version: 1.0
 * @date:2021/7/13
 */
class RuleController extends BaseController
{
    protected $viewPath = 'backend.auth.rule';  //当前控制器所的view所在的目录
    protected $modules = 'auth';        //当前控制器所属模块
    public function __construct(PrtAuthRuleRepository $Repository)
    {
        parent::__construct();
        $this->repositories = $Repository;
    }

    /**
     * 列表展示
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    protected function table(Request $request)
    {
        try {
            $inputs = $request->all();
            $yesOrNo = config('status.yes_no');

            //获取一级分类
            if (empty( $inputs['rule_pid'])) {
                $inputs['rule_pid'] = ZERO;
            }
            $list = $this->repositories->getRows($inputs, 'sort')->toArray();

            $htmlContents = $this->renderHtml('',['list' =>$list,'yn'=>$yesOrNo]);
            if (!empty( $inputs['rule_pid']) && empty($list)) {
                $htmlContents = '';
            }
            return $this->jsonSuccess(['html' => $htmlContents]);
        } catch (CommonException $e) {
            //统一收集错误再做处理
            return $this->jsonFailed($e->getMessage());
        }

    }

    /**
     * 添加/编辑表单
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View|mixed
     */
    protected function form(Request $request)
    {
        try {
            if($request->ajax())
            {
                $row = $this->repositories->getById($request->input('id'));

                $list = $this->repositories->getList([])->toArray();
                //无限级分类
                $treeList = $this->repositories->getTreeList($list,'rule_pid','id','rule_name');
                $treeList = Helper::ListToKV('id','rule_name',$treeList);

                $htmlContents = $this->renderHtml($this->viewPath.'.'.$this->form, ['row' => $row,'treeList'=>$treeList]);
                return $this->jsonSuccess(['html' => $htmlContents]);
            }else{
                return view("admin.tips");
            }
        } catch (CommonException $e) {
            return $this->jsonFailed($e->getMessage());
        }

    }

    /**
     * 保存操作
     * @param RuleRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function save(RuleRequest $request)
    {
        $post = $request->all();
        if(empty($post['sort'])){
            $post['sort'] = ZERO;
        }
        //var_dump($post);exit;
        //获取上级的level
        if ($post['rule_pid'] == 0) {
            $post['level'] = 1;
        } else {
            $info = $this->repositories->getRow(['id' =>$post['rule_pid']])->toArray();
            $post['level'] = intval($info['level']) +1;
        }

        $ret = $this->repositories->save($post);

        if (isset($ret['code'])&&$ret['code']==0){
            return $this->jsonFailed($ret['msg']);
        }
        if ($ret || $ret === 0) {
            return $this->jsonSuccess([]);
        } else {
            return $this->jsonFailed('');
        }
    }
}
